home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Files / deleteEmptyDir / deleteEmptyDir.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  3.5 KB  |  139 lines  |  [TEXT/MPS ]

  1. { deleteEmptyDir and listEmptyDir identify all empty directories on a 
  2.   specified volume.  If the tool is named deleteEmptyDir, the directories
  3.   will be removed.
  4.   
  5.   This purpose of this snippet is to show how easy (hah!) and useful (well,
  6.   yeah) the PBCatSearch call is.  Ask Mark Johnson why this tool might be 
  7.   helpful.
  8.  
  9.   
  10.   Syntax:   deleteEmptyDir <volName>
  11.             listEmptyDir <volName>
  12.             
  13.   Example:  listEmptyDir myHardDisk:
  14.   
  15.   Grobbins 9/91
  16. }
  17.  
  18.  
  19. PROGRAM deleteEmptyDir;
  20.  
  21.     USES
  22.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf, { include it all, why not }
  23.         PasLibIntf,    { standard I/O }
  24.         IntEnv;    { for command line arguments }
  25.         
  26.     CONST
  27.         kMaxFinds = 100;
  28.         kBuffSize = 16384;
  29.                 
  30.     VAR
  31.         toolName, volName:      Str255;
  32.         retCode:    OSErr;
  33.  
  34.         trashDirID: LongInt;
  35.         trashVRefNum: Integer;
  36.         trashedFSSpec: FSSpec;
  37.  
  38.         myHPB: HParamBlockRec;
  39.         loopCtr: INTEGER;
  40.         resultFSSpecArr: PACKED ARRAY[1..kMaxFinds] OF FSSpec;
  41.         CISpec1, CISpec2: CInfoPBRec;
  42.         doneFlag: BOOLEAN;
  43.         buffer: PACKED ARRAY[1..kBuffSize] OF CHAR;
  44.         
  45.     {$S Main}
  46.  
  47. {*----------------*
  48.  | deleteEmptyDir |
  49.  *----------------*}
  50.  
  51.  
  52.         PROCEDURE Fail(retCode: OSErr);
  53.         BEGIN
  54.             IF retCode <> noErr THEN
  55.                 BEGIN
  56.                     WriteLn(Diagnostic, '# ', toolName, ' failed ', retCode);
  57.                     IEexit(retCode);         { exit, returning the appropriate status code}
  58.                 END;
  59.         END;
  60.         
  61.     BEGIN                                                          {main}
  62.         toolName := ArgV^[0]^;
  63.         
  64.         retCode := noErr;
  65.         
  66.         IF ArgC <> 2 THEN 
  67.         BEGIN
  68.             WriteLn(Diagnostic, Concat('### ', toolName, ' - needs 1 parameter'));
  69.             retCode := 2;  { Ord(FP_ParmErrs) = 2 }
  70.             IEexit(retCode);          {exit, returning the appropriate status code}
  71.         END;
  72.         
  73.         
  74.         volName := ArgV^[1]^;
  75.         
  76.         { folder path should end in a colon }
  77.         IF volName[Length(volName)] <> ':' THEN
  78.             volName := Concat(volName, ':');
  79.             
  80.         WriteLn('volume is ', volName);
  81.         
  82.         { now set up parameter blocks for PBCatSearch }
  83.         
  84.         { find vRefNum for folder's disk }
  85.         myHPB.ioCompletion := NIL;
  86.         myHPB.ioNamePtr := @volName;
  87.         myHPB.ioVRefNum := 0;
  88.         myHPB.ioVolIndex := -1;
  89.         retCode := PBHGetVInfo(@myHPB, false);
  90.         IF retCode <> noErr THEN Fail(retCode);
  91.         WriteLn('vRefNum is ', myHPB.ioVRefNum);
  92.         
  93.         { set up parameter blocks to search for all folders with nothing in them }
  94.         myHPB.ioCompletion := NIL;
  95.         myHPB.ioNamePtr := NIL;
  96.         { myHPB.ioVRefNum already set }
  97.         myHPB.ioMatchPtr := FSSpecArrayPtr(@resultFSSpecArr);
  98.         myHPB.ioReqMatchCount := kMaxFinds;
  99.         myHPB.ioSearchBits := fsSBFlAttrib + fsSBDrNmFls;
  100.         myHPB.ioSearchInfo1 := @CISpec1;
  101.         myHPB.ioSearchInfo2 := @CISpec2;
  102.         myHPB.ioSearchTime := -1;
  103.         myHPB.ioCatPosition.initialize := 0;
  104.         myHPB.ioOptBuffer := @buffer;
  105.         myHPB.ioOptBufSize := kBuffSize;
  106.         
  107.         CISpec1.ioNamePtr := NIL;
  108.         CISpec1.ioDrNmFls := 0;
  109.         CISpec1.ioFlAttrib := $10;
  110.         
  111.         CISpec2.ioNamePtr := NIL;
  112.         CISpec2.ioDrNmFls := 0;
  113.         CISpec2.ioFlAttrib := $10;
  114.         
  115.         doneFlag := FALSE;
  116.         REPEAT
  117.             retCode := PBCatSearchSync(@myHPB);
  118.             doneFlag := (retCode = eofErr);
  119.             IF ((retCode = noErr) OR doneFlag) AND (myHPB.ioActMatchCount > 0) THEN
  120.                 { print out names of found folders & maybe delete them }
  121.                 FOR loopCtr := 1 to myHPB.ioActMatchCount DO
  122.                 BEGIN
  123.                     Write(resultFSSpecArr[loopCtr].name);
  124.                     
  125.                     { delete only if name of this tool is deleteEmptyDir }
  126.                     IF toolName = 'deleteEmptyDir' THEN
  127.                     BEGIN
  128.                         retCode := FSpDelete(resultFSSpecArr[loopCtr]);
  129.                         IF retCode <> noErr THEN WriteLn('failed (',retCode,')')
  130.                         
  131.                         ELSE WriteLn('... deleted');
  132.                     END ELSE WriteLn;
  133.                 END;
  134.                 
  135.         UNTIL doneFlag;
  136.  
  137.         IEExit(retCode);
  138.     END.
  139.